angular.module('mvcApp.home', [
    'ngRoute',
    'ngResource'
]).config(['$routeProvider', function($routeProvider) {
    $routeProvider
        .when('/categories', {
            templateUrl: 'static/views/categories.html',
            controller: 'CategoriesCtrl'
        });
    $routeProvider.when('/home', {
        templateUrl: 'static/views/home.html',
        controller: 'HomeCtrl'
    });
}]).controller('CategoriesCtrl', ['$scope', '$http', 'Category',
        function ($scope, $http, Category) {
            $scope.shopCategories = [];
            Category.listShopCategories()
                .then(function (data) {
                    $scope.shopCategories = data;
                    $scope.$apply();
                }).catch(function (e) {
                    console.log(e);
                });
        }])
    .controller('HomeCtrl', ['$scope', '$http', '$location', 'Category', 'Shop', 'Product', 'User',
        function ($scope, $http, $location, Category, Shop, Product, User) {
            $scope.toSearchType = 'shop';
            $scope.searchCommon = function(keyword) {
                if($scope.toSearchType === 'shop') {
                    $scope.searchShops(keyword);
                } else {
                    $scope.searchProducts(keyword);
                }
            };
            $scope.shopCategories = [];
            $scope.shopCategoryGroups = [];
            Category.listShopCategories()
                .then(function (data) {
                    $scope.shopCategories = data;
                    $scope.shopCategoryGroups = _.values(_.groupBy($scope.shopCategories, function (item, index) {
                        return parseInt(index / 10);
                    }));
                    $scope.$apply();
                }).catch(function (e) {
                    console.log(e);
                });
            $scope.hotestShops = [];
            $scope.hotestShopsGroups = [];
            Shop.getHotestShops()
                .then(function (shops) {
                    $scope.hotestShops = shops;
                    $scope.hotestShopsGroups = _.values(_.groupBy(shops, function (item, index) {
                        return parseInt(index / 3);
                    }));
                    $scope.$apply();
                }).catch(function (err) {
                    console.log(err);
                });

            $scope.hotestProducts = [];
            Product.getHotestProducts(3)
                .then(function (products) {
                    $scope.hotestProducts = products;
                    $scope.$apply();
                }).catch(function (err) {
                    console.log(err);
                });
            $scope.searchShops = function (keyword) {
                if (!keyword) {
                    return;
                }
                $location.path('/search-shops/' + keyword);
            };
            $scope.showProductPage = function (product) {
                $location.path('/product/' + product.id);
            };
            $scope.searchProducts = function (keyword) {
                if (!keyword) {
                    return;
                }
                $location.path('/search-products/' + keyword);
            };
            $scope.index_background_image = 'http://p2.wmpic.me/article/2015/05/08/1431048328_BXxjuoyp.gif';
            $scope.index_background_images = ['http://p2.wmpic.me/article/2015/05/08/1431048328_BXxjuoyp.gif'];
            function loadIndexBackgroundImage() {
                User.getConfig('index_background_image')
                    .then(function (url) {
                        if (url) {
                            $scope.index_background_image = url;
                            $scope.index_background_images = _.map(url.split(','), function(u){return {url: u};});
                            $scope.$applyAsync();
                        }
                    }).catch(function (err) {
                        console.log(err);
                    });
            }

            loadIndexBackgroundImage();
        }]);